import L, { map } from 'leaflet'
import tool from '@/utils/tool.js'
import controlApi from '@/api/monitor/control.js'
import moment from 'moment'
import '@/static/js/leaflet/makerIconLayer.js'
// import { getColorIndex, getlegendOptions, getlegend, getTime } from './liveEv.js'
module.exports = {
	data() {
		return {}
	},
	methods: {
		// 雨情 useCacheData使用缓存
		async initBydefault(isActive, useCacheData = false, type) {
			console.info(`isActive:`, isActive)
			try {
				if (!isActive && this.layerGroups.moreEle) {
					this.layerGroups.moreEle.clearLayers()
					this.setTitle('', 'rain', 'del')
					this.rainValue = 0
					this.filterIndex = 0
					return
				}
				if (this.layerGroups.moreEle) {
					this.layerGroups.moreEle.clearLayers()
				}
				this.$nextTick(() => {
					this.$refs.simpleLegend[0].setIndex(this.filterIndex)
				})

				let getUserInfoData = uni.getStorageSync('getUserInfoData')
				let areaCode = getUserInfoData.dept.code
				let areaLevel = getUserInfoData.dept.areaLevel
				let obj = {
					0: 'CITY',
					1: 'COUNTY',
					2: 'TOWNSHIP'
				}
				let siteList = []
				if (useCacheData) {
					siteList = this.cache_resp
				} else {
					uni.showLoading({
						title: '正在加载'
					})
					this.loading = true

					const resp = await controlApi.GetExtremumSiteNoAndValue({
						StartTime: moment().subtract(this.accHour, 'hours').format('YYYY-MM-DD HH:mm'), //'2023-07-20 10:00:00',
						EndTime: moment().format('YYYY-MM-DD HH:mm'), //'2023-07-21 10:00:00',
						Elements: [type],
						AreaCode: '350400',
						// SiteCategories: ['0'],
						SpotDataType: '1',
						legend: tool.getLegendItem(type)
					})
					console.info(`resp:`, resp)
					siteList = resp.data.data
					this.cache_resp = siteList
					this.$hideLoading()
					this.loading = false
				}

				// .collision({ margin: 20 })
				let siteLayer = L.makerIconLayer({
					minNameShowZoom: 8.7,
					showClassName: '.rain-popup-name'
				}).addTo(this.layerGroups.moreEle)
				let legend = tool.getLegendItem(type)
				// 打点
				siteList.forEach((el) => {
					const vals = Object.values(el.vals)
					let firstVal = {}
					if (vals.length > 0) {
						firstVal = vals[0]
					}
					if (firstVal.val < this.rainValue) {
						return
					}
					const { bgColor, index } = tool.getColorIndex(legend, firstVal.val)
					let fontColor = '#000' // 默认黑色

					let html = `
								<div class='map-town-legend rain-popup  rain-popup-name' style='background:${bgColor};color:${fontColor};display:none'>
										<span class='fontBorder'> ${el.siteName}  ${firstVal.val}${legend.unit}</span>
								   <div class='borderTop' style='background:${bgColor}'></div>
								       </div>
								   <div class='circle-legend'  style='background:${bgColor}'></div>
								   `
					L.marker([el.lat, el.lon], {
						icon: L.divIcon({
							className: 'map-town-legend-style',
							html: html
						})
					})
						.on('click', (e) => {
							this.ecTpye = 'moreEle'
							this.nodeId = el
							this.sitePopupShow = true
						})

						.addTo(siteLayer)
				})
				siteLayer.initStart()
				// 	let domList = document.querySelectorAll('.rain-popup')
				// domList = Array.from(domList)
				// const evAdd = () => {
				// 	// 市级显示
				// 	if (this.map._zoom < 8.7 && this.rain_zoom > 8.7) {
				// 		this.rain_zoom = this.map._zoom
				// 		let domList = document.querySelectorAll('.rain-popup')
				// 		domList = Array.from(domList)
				// 		domList.forEach((el) => (el.style.display = 'none'))
				// 	}

				// 	if (this.map._zoom > 8.7 && (this.rain_zoom < 8.7 || this.map._zoom == this.rain_zoom)) {
				// 		this.rain_zoom = this.map._zoom
				// 		let domList = document.querySelectorAll('.rain-popup')
				// 		domList = Array.from(domList)
				// 		domList.forEach((el) => (el.style.display = 'flex'))
				// 	}
				// }
				// if (!this.rain_zoom) {
				// 	this.rain_zoom = this.map._zoom
				// }
				// evAdd()
				// this.map.on('zoomend', (e) => {
				// 	if (this.type != 'moreEle') {
				// 		// this.map.off('zoomend')
				// 		return
				// 	}
				// 	evAdd()
				// })
				this.setTitle((moment().format('YYYY-MM-DD HH:mm') || '') + ' ' + legend.desc, 'rain')
			} catch (error) {
				console.error(error)
				this.$hideLoading()
				this.loading = false
				// uni.showToast({
				// 	icon: 'error',
				// 	title: '获取失败'
				// })
			}
		}
	},
	computed: {},
	mounted() {}
}
